package com.astronomy.user.service;

import com.astronomy.entity.IdWorker;
import com.astronomy.user.mapper.UserMapper;
import com.astronomy.user.pojo.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 全部数据
     * @return
     */
    public List<User> findAll(){
        return userMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public User findById(String id){
        return  userMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     * @param user
     */
    public void add(User user){
        user.setId(idWorker.nextId()+"");
        userMapper.insert(user);
    }

    /**
     * 修改
     * @param user
     */
    public void update(User user){
        userMapper.updateByPrimaryKey(user);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        userMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public Page<User> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<User>)userMapper.selectAll();
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<User> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return userMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // ID
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
           	}
            // 密码
            if(searchMap.get("password")!=null && !"".equals(searchMap.get("password"))){
                criteria.andLike("password","%"+searchMap.get("password")+"%");
           	}
            // 昵称
            if(searchMap.get("nickname")!=null && !"".equals(searchMap.get("nickname"))){
                criteria.andLike("nickname","%"+searchMap.get("nickname")+"%");
           	}
            // 性别
            if(searchMap.get("sex")!=null && !"".equals(searchMap.get("sex"))){
                criteria.andLike("sex","%"+searchMap.get("sex")+"%");
           	}
            // 头像
            if(searchMap.get("avatar")!=null && !"".equals(searchMap.get("avatar"))){
                criteria.andLike("avatar","%"+searchMap.get("avatar")+"%");
           	}
            // E-Mail
            if(searchMap.get("email")!=null && !"".equals(searchMap.get("email"))){
                criteria.andLike("email","%"+searchMap.get("email")+"%");
           	}
            // 兴趣
            if(searchMap.get("interest")!=null && !"".equals(searchMap.get("interest"))){
                criteria.andLike("interest","%"+searchMap.get("interest")+"%");
           	}
            // 个性
            if(searchMap.get("personality")!=null && !"".equals(searchMap.get("personality"))){
                criteria.andLike("personality","%"+searchMap.get("personality")+"%");
           	}

            // 粉丝数
            if(searchMap.get("fanscount")!=null ){
                criteria.andEqualTo("fanscount",searchMap.get("fanscount"));
            }
            // 关注数
            if(searchMap.get("followcount")!=null ){
                criteria.andEqualTo("followcount",searchMap.get("followcount"));
            }

        }
        return example;
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    public Page<User> findPage(Map<String, Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<User>)userMapper.selectByExample(example);
    }

}
